home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: lut.ac.uk!usenet
- From: COSJM1 <S.J.Morgan-92@student.lut.ac.uk>
- Subject: Boring template problem again
- Sender: usenet@lut.ac.uk (Usenet-News)
- Message-ID: <DMzpFy.Mo8@lut.ac.uk>
- Date: Sun, 18 Feb 1996 21:09:34 GMT
- X-Nntp-Posting-Host: co-pclab27.lut.ac.uk
- Content-Transfer-Encoding: 7bit
- Content-Type: text/plain
- Mime-Version: 1.0
- X-Mailer: Mozilla 1.2N (Windows; I; 16bit)
- Organization: Loughborough University
-
- Hi,
- With reference to my previous post, thanks for the replies, the
- trouble is, they seem to provide various levels of success but none of them means the code links correctly under CFront 3.0 and GCC 2.7.0.
-
- To reinterate my problem, given the following:
-
- IN FILE "template.h":
-
- template<class T> class B
- {
- public:
- B() {}
- void test(void);
- };
-
- IN FILE "template1.cpp":
-
- #include "template.h"
-
- void test_again( void );
-
- main()
- {
- B<int*>
- test;
-
- test.test();
-
- test_again();
- }
-
- IN FILE "template2.cpp":
-
- #include "template.h"
-
- void test_again( void )
- {
- B<int*>
- test;
-
- test.test();
- }
-
- IN FILE "implem.cpp":
-
- #include <iostream.h>
-
- #include "template.h"
-
- template<class T> void B<T>::test(
- void )
- {
- cout << "hello";
- }
-
- the linker reports the following error:
-
- (Error) Undefined symbol(s)
- B<int*>::test(void), referred to from template1.o
-
- It was suggested that I include the member function definition after the
- class as below:
-
- IN FILE "template.h":
-
- #include <iostream.h>
-
- template<class T> class B
- {
- public:
- B() {}
- void test(void);
- };
-
- template<class T> void B<T>::test(
- void )
- {
- cout << "hello";
- }
-
- IN FILE "template1.cpp"::
-
- #include "template.h"
-
- void test_again( void );
-
- main()
- {
- B<int*>
- test;
-
- test.test();
-
- test_again();
- }
-
- IN FILE "template2.cpp"::
-
- #include "template.h"
-
- void test_again( void )
- {
- B<int*>
- test;
-
- test.test();
- }
-
- This time the following link error reported is:
-
- (Error) Global test__11B__pt__3_PiFV multiply defined (in template2 and template1)
-
- by CFront 3.0, but is linked correctly under GCC(g++) 2.7.0.
-
- If I do the following, it links under CFront but not GCC:
-
- IN FILE "template.h":
-
- template<class T> class B
- {
- public:
- B() {}
- void test(void);
- };
-
- // force instantiation
- extern B<int*> __test__;
-
- IN FILE "template1.cpp":
-
- #include "template.h"
-
- void test_again( void );
-
- main()
- {
- B<int*>
- test;
-
- test.test();
-
- test_again();
- }
-
- IN FILE "template2.cpp":
-
- #include "template.h"
-
- void test_again( void )
- {
- B<int*>
- test;
-
- test.test();
- }
-
- IN FILE "implem.cpp":
-
- #include <iostream.h>
-
- #include "template.h"
-
- template<class T> void B<T>::test(
- void )
- {
- cout << "hello";
- }
-
- The trouble is the code must be portable - does anybody know the best
- approach I should follow.
-
- Thank you again for any advice given.
-
-
-